package com.wyj.wuliwuli.service.impl;

import com.wyj.wuliwuli.common.AjaxRequest;
import com.wyj.wuliwuli.common.AjaxResult;
import com.wyj.wuliwuli.common.PageParam;
import com.wyj.wuliwuli.entity.WuliUserVideoLike;
import com.wyj.wuliwuli.entity.WuliVideo;
import com.wyj.wuliwuli.entity.mapper.WuliUserVideoLikeMapper;
import com.wyj.wuliwuli.entity.mapper.WuliVideoMapper;
import com.wyj.wuliwuli.service.WuliUserVideoLikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Optional;

@Service
@RequiredArgsConstructor
public class WuliUserVideoLikeServiceImpl implements WuliUserVideoLikeService {

    private final WuliUserVideoLikeMapper wuliUserVideoLikeMapper;
    private final WuliVideoMapper wuliVideoMapper;

    /**
     * 分页查询
     *
     * @param ajaxRequest
     * @return
     */
    @Override
    public AjaxResult page(AjaxRequest<PageParam> ajaxRequest) {
        PageParam pageParam = ajaxRequest.getData();
        Pageable pageable = PageRequest.of(pageParam.getPageNum(), pageParam.getPageSize(), pageParam.getDirection(), pageParam.getProperty());
        Page<WuliUserVideoLike> page = wuliUserVideoLikeMapper.findAll(pageable);
        return AjaxResult.ok(page);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult getById(Long id) {
        if (id == null) {
            return AjaxResult.fail("id不能为空");
        }
        Optional<WuliUserVideoLike> optional = wuliUserVideoLikeMapper.findById(id);
        if (!optional.isPresent()) {
            return AjaxResult.fail("查询的id不存在或已被删除");
        }
        return AjaxResult.ok(optional.get());

    }

    /**
     * 根据userId查询
     *
     * @param userId
     * @return
     */
    @Override
    public AjaxResult getByUserId(Long userId) {
        if (userId == null) {
            return AjaxResult.fail("userId不能为空");
        }

        List<WuliUserVideoLike> list = wuliUserVideoLikeMapper.findAllByUser_Id(userId);

        return AjaxResult.ok(list);

    }

    /**
     * 保存
     *
     * @param ajaxRequest
     * @return
     */
    @Override
    public AjaxResult save(AjaxRequest<WuliUserVideoLike> ajaxRequest) {
        WuliUserVideoLike wuliUserVideoLike = ajaxRequest.getData();
        if (wuliUserVideoLike == null) {
            return AjaxResult.fail("用户点赞不能为空");
        }
        if (wuliUserVideoLike.getUser() == null) {
            return AjaxResult.fail("点赞用户不能为空");
        }
        if (wuliUserVideoLike.getVideo() == null) {
            return AjaxResult.fail("点赞视频不能为空");
        }
        List<WuliUserVideoLike> list = wuliUserVideoLikeMapper.findAllByUser_IdAndVideo_Id(wuliUserVideoLike.getUser().getId(),wuliUserVideoLike.getVideo().getId());
        if (!CollectionUtils.isEmpty(list)) {
            for (WuliUserVideoLike like:list) {
                wuliUserVideoLikeMapper.deleteById(like.getId());
            }
            WuliVideo video = wuliVideoMapper.getById(wuliUserVideoLike.getVideo().getId());
            video.setLikeCount(video.getLikeCount() == null ?0:(video.getLikeCount()-1) );
            if (video.getLikeCount() < 0) {
                video.setLikeCount(0L);
            }
            wuliVideoMapper.save(video);

            return AjaxResult.of();
        }
        wuliUserVideoLikeMapper.save(wuliUserVideoLike);
        WuliVideo video = wuliVideoMapper.getById(wuliUserVideoLike.getVideo().getId());
         video.setLikeCount(video.getLikeCount() == null ?1:(video.getLikeCount()+1) );
        if (video.getLikeCount() < 0) {
            video.setLikeCount(0L);
        }
         wuliVideoMapper.save(video);

        return AjaxResult.of();

    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public AjaxResult deleteById(Long id) {
        if (id == null) {
            return AjaxResult.fail("id不能为空");
        }
        WuliUserVideoLike wuliUserVideoLike = wuliUserVideoLikeMapper.getById(id);
        WuliVideo video = wuliUserVideoLike.getVideo();
        video.setLikeCount(video.getLikeCount() == null ?0:(video.getLikeCount()-1) );
        if (video.getLikeCount() < 0) {
            video.setLikeCount(0L);
        }
        wuliVideoMapper.save(video);
        if (wuliUserVideoLikeMapper.existsById(id)) {
            wuliUserVideoLikeMapper.deleteById(id);
        }else {
           return AjaxResult.fail("用户输入的id不存在或已被删除");
        }

        return AjaxResult.of();
    }
}
